/* 线程局部存储测试 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

static __thread char buf[100];

static void *thread_start(void *arg)
{
    strcpy(buf, "Child Thread\n");
    printf("子线程:buf(%p)=%s", buf, buf);
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    pthread_t tid;
    int ret;

    strcpy(buf, "Main Thread\n");

    /* 创建子进程 */
    ret = pthread_create(&tid, NULL, thread_start, NULL);
    if (ret != 0)
    {
        printf("pthread_create error: %d\n", ret);
        return -1;
    }

    /* 等待子进程结束 */
    if (pthread_join(tid, NULL)) {
        fprintf(stderr, "pthread_join error: %d\n", ret);
        exit(-1);
    }

    printf("主线程:buf(%p)=%s", buf, buf);
    exit(0);
}